<?php
require_once "../../php/config/config.php";
require_once "../model/adminDb.php";
$AdminDb = new AdminDb();

$name = $_SESSION['name'];
$dat = $AdminDb->query_userName($name);
$card = $dat[0]['card'];


?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/js/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/css/view.css"/>
    <title>管理后台</title>
    <style>
        #sumbitPerson {
            float: right;
        }

        #selectPerson {
            display: none;
        }

        #selectBox {
            margin-left: 0px;
        }

        .statusBox {
            width: 100%;
            color: white;
            background-color: #F0F2F5;
        }

    </style>

</head>
<body class="layui-view-body">
<div class="layui-content">
    <div class="layui-row">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="form-box">
                    <table id="bugList" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="layui-content" id="selectPerson">
    <div class="layui-row">
        <div class="layui-card">
            <div class="layui-card-header">添加项目：</div>
            <form class="layui-form layui-card-body" action="">

                <div class="layui-form-item">
                    <label class="layui-form-label">项目名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="projectName" id="projectName" required lay-verify="projectName" placeholder="请输入项目名称"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block" style="float: right">
                        <a class="layui-btn layui-btn-blue" id="saveProject" onclick="saveProject()" lay-submit
                           lay-filter="saveProject">立即提交</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script id="toolbarDemo" type="text/html">
    <div class="layui-btn-container">
        <?php
        echo "<button class=\"layui-btn layui-btn-sm layui-btn-primary\" onclick=\"addProject()\"><i class=\"layui-icon\">&#xe608;</i>
            新增项目
        </button>";

        ?>

        <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="refresh()"><i class="layui-icon">&#xe9aa;</i>
            刷新
        </button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <?php

    echo " <a class=\"layui-btn layui-btn-xs  layui-btn-danger\" lay-event=\"del\">删除</a>";
    ?>

</script>
<script src="../../static/js/jquery-3.3.1.min.js"></script>
<script src="../../static/js/layui/layui.all.js"></script>
<script src="../../static/js/config.js"></script>
<script>
    $(document).ready(function () {
        var global_win="";
        table();

    });

    //表格数据查询
    function table() {
        layui.use('table', function () {
            var table = layui.table;
            //第一个实例
            table.render({
                elem: '#bugList'
                , url: '../controller/project.php' //数据接口
                , page: true //开启分页
                , toolbar: "#toolbarDemo"
                , method: 'post'
                , limits: [20, 40, 60, 100]
                , cols: [[ //表头
                    {field: 'index', title: '序号', fixed: 'left', type: 'numbers'}
                    , {field: 'name', title: '项目名称', width: 100}

                    , {field: '', toolbar: '#barDemo',width:250,fixed:'right', title: '操作'}
                ]]
                , done: function (res, curr, count) {

                    console.log(res);
                    if (res.count == 0) {


                        layer.msg("没有符合条件的数据哦");
                    }
                }
            });


            //监听行工具事件
            table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'detail') {
                    $id = data.id;//获取当前一行的id
                    window.open('show_bug.php?id=' + $id, '_blank');
                } else if (layEvent === 'del') {
                    layer.confirm('真的删除行么', function (index) {

                        $id = data.id;//获取当前一行的id
                        delProject($id, index, obj);

                        //向服务端发送删除指令
                    });
                }
            });


        });
    }

    function addProject() {

        personWin = layer.open({
            type: 1,
            shade: false,
            area: ['50%', '30%'], //宽高
            title: false, //不显示标题
            content: $('#selectPerson'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                $("#selectPerson").css({"display": "none"});
            }

        });

    }

    function saveProject() {
        var project = $("#projectName").val();
        if (project != "") {
            $.ajax({
                type: "post",
                cache: "false",
                url: '../controller/add_project.php', //数据接口
                data: {
                    "project": project,
                },
                dataType: "json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg('添加成功！', {time: 5000, icon: 6});
                        refresh();
                    }
                    else {
                        layer.msg(res.msg, {time: 5000, icon: 0});
                    }
                }, error: function (res) {
                    layer.msg('添加失败！', {time: 5000, icon: 0});
                }

            });
        }
        else {
            layer.msg("项目名称不能为空", {time: 5000, icon: 0});
        }


    }
    //删除信息
    function delProject(delId) {
        $.ajax({
            type: 'post',
            cache: "false",
            url: '../controller/del_project.php',
            data: {
                "id": delId,
            }
            , dataType: "json",
            success: function (res) {
                if (res.code == 200) {
                    layer.msg("成功删除");
                    refresh();
                }
                else {
                    layer.msg(res.msg);
                }

            },
            error: function (res) {
                layer.msg("操作失败");
            }


        })
    }
</script>

</body>


</html>